import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        track: {
            url: null,
            name: null,
            author: null,
            album: null
        },
        audio: {
            name: null,
            url: null,
            cover: null
        }
    },
    mutations: {
        setPlayTrack(state, track) {
            state.track = track
            state.audio = track
            state.audio.cover = track.album
        }
    },
    actions: {
        async setPlayTrackAction({commit}, payload) {
            let track = {}
            await axios.get(`/api/music/url?id=${payload.id}`).then(response => {
                track.url = response.data.data[0].url
            })
            await axios.get(`/api/song/detail?ids=${payload.id}`).then(response => {
                track.name = response.data.songs[0].name
                track.author = response.data.songs[0].ar
                track.album = response.data.songs[0].al
            })
            commit('setPlayTrack', track)
        },
    }
})